package avicit.bdp.dcs.plugin.iotdb;

import avicit.bdp.dcs.plugin.BaseDataxParam;
import avicit.bdp.dcs.plugin.DataxConstants;
import avicit.bdp.dcs.plugin.DataxJsonInterface;
import avicit.bdp.dcs.plugin.iotdb.param.IotdbDataxParam;
import avicit.bdp.dcs.utils.JdbcConstants;
import com.google.common.collect.ImmutableList;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-08-01 11:39
 * @类说明：
 * @修改记录：
 */
public class IotdbDataxJson implements DataxJsonInterface {

    /**
     * 获取Reader插件名称
     * @return
     */
    @Override
    public String getReaderName() {
        return null;
    }

    /**
     * 获取Writer插件名称
     * @return
     */
    @Override
    public String getWriterName() {
        return DataxConstants.IOTDB_WRITER;
    }

    @Override
    public Map<String, Object> buildReader(BaseDataxParam param) {
        return null;
    }

    @Override
    public Map<String, Object> buildWriter(BaseDataxParam param) {
        IotdbDataxParam dataxParam = (IotdbDataxParam) param;

        Map<String, Object> writerObj = new HashMap<>(4);
        writerObj.put(DataxConstants.NAME, getWriterName());
        Map<String, Object> parameterObj = new HashMap<>(8);

        parameterObj.put(DataxConstants.IOTDB_BOOTSTRAP_SERVERS, dataxParam.getBootstrapServers());
        parameterObj.put(DataxConstants.IOTDB_USERNAME, dataxParam.getUsername());
        parameterObj.put(DataxConstants.IOTDB_PASSWORD, dataxParam.getPassword());
        parameterObj.put(DataxConstants.IOTDB_BATCH_SIZE, dataxParam.getBatchSize());
        if(CollectionUtils.isNotEmpty(dataxParam.getPreSql())) {
            parameterObj.put(DataxConstants.IOTDB_PRE_SQL, ImmutableList.of(dataxParam.getPreSql()));
        }

        if(JdbcConstants.HDFS.equals(dataxParam.getSourceType())){
            parameterObj.put(DataxConstants.IOTDB_DEVICEID, dataxParam.getDeviceId());
            parameterObj.put(DataxConstants.IOTDB_PARAS, dataxParam.getParas());
        }else if(JdbcConstants.KAFKA.equals(dataxParam.getSourceType())){
            parameterObj.put(DataxConstants.IOTDB_KAFKA_2IOT_MAP, dataxParam.getKafka2IotMap());
            parameterObj.put(DataxConstants.IOTDB_COLUMNS, dataxParam.getWriterColumns());
            parameterObj.put(DataxConstants.TYPE, dataxParam.getSourceType());
        }

        writerObj.put(DataxConstants.PARAMETER, parameterObj);
        return writerObj;
    }

}
